/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Util;

import com.webaccess.retailws.Bean.UserAddressBean;
import com.webaccess.retailws.Bean.UserInfoBean;
import com.webaccess.retailws.Bean.MailBean;
import com.webaccess.retailws.Delegate.MailDelegate;
import com.webaccess.retailws.Delegate.UserInfoDelegate;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Owner
 */
public class UserAccountUtil {

    UserInfoBean userInfoBeanObj = null;
    UserAddressBean userAddrBean = null;
    UserInfoDelegate userDelegate = null;
    MailDelegate mailDelegateObj = null;
    List listAddress = null;
    MailBean mailInfo = null;
    SendMailUtil mailUtil = null;
    CommonUtil commUtil = null;

    public UserAccountUtil() {
    }

    public String formatStringDate(String birthDate) {
        String formatDateString = null, chkDay = null, chkMonth = null, chkYear = null;
        try {
//            chkDay = chkDate.substring(0, 2) ;
//            chkMonth = chkDate.substring(3, 5);
//            chkYear = chkDate.substring(6, 10);
            formatDateString = birthDate.substring(6, 10) + "-" + birthDate.substring(3, 5) + "-" + birthDate.substring(0, 2);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return formatDateString;
    }

    public boolean checkSpecialChar(String username) {
        String specialChar = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?~";
        for (int i = 0; i < username.length(); i++) {
            if (specialChar.indexOf(username.charAt(i)) != -1) {
                return true;
            }
        }
        return false;
    }

    public String saveUserInfo(String prefix, String firstName, String middleName, String lastName, String profession, String emailId,
            String mobileNo, String dateOfBirth, int newsLetter, Long securityQuestionId, String answer, String username, String password,
            String billingHouse, String billingStreet, String billingCity, String billingState, String billingPinCode, String billingContact,
            String deliveryHouse, String deliveryStreet, String deliveryCity, String deliveryPinCode, String deliveryState, String deliveryContact) {

        listAddress = new ArrayList();
        UserAddressBean[] userAddrBeanList = null;
        userInfoBeanObj = new UserInfoBean();
        mailDelegateObj = new MailDelegate();
        userDelegate = new UserInfoDelegate();
        mailUtil = new SendMailUtil();
        commUtil = new CommonUtil();
        String SUCCESS = "registersuccess";
        try {
            listAddress.clear();
            userInfoBeanObj.setPrefix(prefix);
            userInfoBeanObj.setFirstName(firstName);
            userInfoBeanObj.setMiddleName(middleName);
            userInfoBeanObj.setLastName(lastName);
            userInfoBeanObj.setFullName(prefix + " " + firstName + " " + middleName + "" + lastName);
            userInfoBeanObj.setEmailId(emailId);
            userInfoBeanObj.setMobileNo(mobileNo);
            userInfoBeanObj.setDateOfBirth(dateOfBirth);
            userInfoBeanObj.setProfession(profession);
            userInfoBeanObj.setNewsLetter(newsLetter);
            userInfoBeanObj.setSecurityQuestionId(securityQuestionId);
            userInfoBeanObj.setAnswer(answer);
            userInfoBeanObj.setUserName(username);
            userInfoBeanObj.setPassword(EncryptDecryptUtil.encrypt(password));
            userInfoBeanObj.setInfoSaveEditDate(commUtil.getCurrentDateTime());
            userAddrBean = new UserAddressBean();
            userAddrBean.setHouse(billingHouse);
            userAddrBean.setStreet(billingStreet);
            userAddrBean.setCity(billingCity);
            userAddrBean.setState(billingState);
            userAddrBean.setPinCode(billingPinCode);
            userAddrBean.setContact(billingContact);
            userAddrBean.setAddrTitle(prefix);
            userAddrBean.setAddrFirstName(firstName);
            userAddrBean.setAddrMiddleName(middleName);
            userAddrBean.setAddrLastName(lastName);
            listAddress.add(userAddrBean);
            userAddrBean = new UserAddressBean();
            userAddrBean.setHouse(deliveryHouse);
            userAddrBean.setStreet(deliveryStreet);
            userAddrBean.setCity(deliveryCity);
            userAddrBean.setState(deliveryState);
            userAddrBean.setPinCode(deliveryPinCode);
            userAddrBean.setContact(deliveryContact);
            userAddrBean.setAddrTitle(prefix);
            userAddrBean.setAddrFirstName(firstName);
            userAddrBean.setAddrMiddleName(middleName);
            userAddrBean.setAddrLastName(lastName);
            listAddress.add(userAddrBean);
            userAddrBeanList = (UserAddressBean[]) listAddress.toArray(new UserAddressBean[0]);
            //userInfoBeanObj.setAddress(userAddrBean);
            Long userId = userDelegate.insertUserInfo(userInfoBeanObj, userAddrBeanList);
            if (userId > 0) {
                // Send Welcome Mail functionality
                StringBuffer messageBody = new StringBuffer();
                messageBody.append("E Shop Registration Info");
                messageBody.append("\n\n");
                messageBody.append("Welcome to E-Shop Site");
                messageBody.append("\n\n");
                messageBody.append("Username :");
                messageBody.append(userInfoBeanObj.getUserName());
                messageBody.append("\n");
                messageBody.append("Password :");
                messageBody.append(password);
                messageBody.append("\n");
                messageBody.append("Name :");
                messageBody.append(userInfoBeanObj.getFullName());
                messageBody.append("\n");
                messageBody.append("Email Id :");
                messageBody.append(userInfoBeanObj.getEmailId());
                messageBody.append("\n");
                messageBody.append("Account Creation Date :");
                messageBody.append(userInfoBeanObj.getInfoSaveEditDate());
                messageBody.append("\n");
                mailInfo = new MailBean();
                mailInfo.setFromAddress("abhishek.agrawal@webaccess.co.in");
                mailInfo.setToAddress(userInfoBeanObj.getEmailId());
                mailInfo.setMsgSubject("E Shop - Registration Info");
                mailInfo.setMsgContent(messageBody.toString());
                mailInfo.setMsgDateTime(commUtil.getCurrentDateTime());
                mailInfo.setUserId(userId);
                mailInfo.setPurpose("User Registration");

                Long mailId = mailDelegateObj.saveMailInfo(mailInfo);
                if (mailId > 0) {
                    mailUtil.sendMail(mailInfo.getFromAddress(), mailInfo.getToAddress(), mailInfo.getMsgSubject(), mailInfo.getMsgContent());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return SUCCESS;
        }
    }

    public String saveAddressInfo(Long userId, Long addressId, String editPrefix, String editFirstName, String editMiddleName, String editLastName, String editHouse, String editStreet, String editCity, String editPinCode, String editState, String editContact) {
        listAddress = new ArrayList();
        UserAddressBean[] userAddrBeanList = null;
        userDelegate = new UserInfoDelegate();
        mailUtil = new SendMailUtil();
        String SUCCESS = "";

        try {
            listAddress.clear();
            userAddrBean = new UserAddressBean();
            userAddrBean.setHouse(editHouse);
            userAddrBean.setStreet(editStreet);
            userAddrBean.setCity(editCity);
            userAddrBean.setState(editState);
            userAddrBean.setPinCode(editPinCode);
            userAddrBean.setContact(editContact);
            userAddrBean.setAddrTitle(editPrefix);
            userAddrBean.setAddrFirstName(editFirstName);
            userAddrBean.setAddrMiddleName(editMiddleName);
            userAddrBean.setAddrLastName(editLastName);
            listAddress.add(userAddrBean);
            userAddrBeanList = (UserAddressBean[]) listAddress.toArray(new UserAddressBean[0]);
            boolean addressSaved = userDelegate.saveAddress(addressId, userAddrBeanList);
            if (addressSaved == true) {
                SUCCESS = "showacctdashboard";
            } else {
                SUCCESS = "showeditaddress";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return SUCCESS;
        }
    }

    public String savePersonalInfo(Long userId, String prefix, String firstName, String middleName, String lastName, String profession, String emailId, String dateOfBirth, String mobileNo, String newPassword) {
        boolean personalInfoSaved = false;
        userInfoBeanObj = new UserInfoBean();
        userDelegate = new UserInfoDelegate();
        mailUtil = new SendMailUtil();
        commUtil = new CommonUtil();
        String SUCCESS = "";
        try {

            userInfoBeanObj.setPrefix(prefix);
            userInfoBeanObj.setFirstName(firstName);
            userInfoBeanObj.setMiddleName(middleName);
            userInfoBeanObj.setLastName(lastName);
            userInfoBeanObj.setFullName(prefix + " " + firstName + " " + middleName + "" + lastName);
            userInfoBeanObj.setEmailId(emailId);
            userInfoBeanObj.setMobileNo(mobileNo);
            userInfoBeanObj.setDateOfBirth(dateOfBirth);
            userInfoBeanObj.setProfession(profession);
            if (!newPassword.equals("")) {
                userInfoBeanObj.setPassword(EncryptDecryptUtil.encrypt(newPassword));
            }
            userInfoBeanObj.setInfoSaveEditDate(commUtil.getCurrentDateTime());

            personalInfoSaved = userDelegate.savePersonalInfo(userId, userInfoBeanObj);
            if (personalInfoSaved == true) {
                SUCCESS = "editaccountsuccess";
            } else {
                SUCCESS = "editaccountfailure";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return SUCCESS;
        }
    }
}
